package com.myccb.app.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.myccb.app.AppStatus;
import com.myccb.app.view.activity.LoginActivity;
import com.myccb.app.R;
import com.myccb.app.base.BaseFragment;
import com.myccb.app.widget.rollpageview.ColorPointHintView;
import com.myccb.app.widget.rollpageview.LoopPagerAdapter;
import com.myccb.app.widget.rollpageview.RollPagerView;
import com.myccb.app.widget.rollpageview.StaticPagerAdapter;

/**
 * Created by hyt on 2017/11/24.
 */

public class CreditCardFragment extends BaseFragment {
    private static CreditCardFragment instance;
    private RollPagerView adsRollPage;
    private View[] headerView = new View[3];
    private LinearLayout creditCardDetailsLayout;

    public static CreditCardFragment getInstance() {
        if (instance == null)
            instance = new CreditCardFragment();
        return instance;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_creditcard, null);
        initView(view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isCreate())
        if (AppStatus.isLogin){
            switchHeaderLayout(2);
        }
        else {
            switchHeaderLayout(1);
        }
    }

    private void initView(View view) {
        adsRollPage = view.findViewById(R.id.creditcard_mainpage_ads);

        ImageView loginBtn = view.findViewById(R.id.creditcard_mainpage_login);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), LoginActivity.class);
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });

        headerView[0] = view.findViewById(R.id.creditcard_headerlayout_normal);
        headerView[1] = view.findViewById(R.id.creditcard_headerlayout_nologin);
        headerView[2] = view.findViewById(R.id.creditcard_headerlayout_nocreditcard);

        creditCardDetailsLayout = view.findViewById(R.id.creditcard_details);

        adsRollPage.setAdapter(new LoopPagerAdapter(adsRollPage) {
            private int[] adImgs = {
                    R.mipmap.creditcard_mainpage_adv_1, R.mipmap.creditcard_mainpage_adv_2,
                    R.mipmap.creditcard_mainpage_adv_3, R.mipmap.creditcard_mainpage_adv_4
            };

            @Override
            public View getView(ViewGroup container, int position) {
                ImageView view = new ImageView(container.getContext());
                view.setImageResource(adImgs[position]);
                view.setScaleType(ImageView.ScaleType.CENTER_CROP);
                view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                return view;
            }

            @Override
            public int getRealCount() {
                return adImgs.length;
            }
        });


        ViewGroup creditCardManageLayout = (ViewGroup) View.inflate(getContext(), R.layout.creditcard_mainpage_manage, null);
        final View[] creditCardManagePages = new View[creditCardManageLayout.getChildCount()];
        for (int i = 0; i < creditCardManageLayout.getChildCount(); i++)
            creditCardManagePages[i] = creditCardManageLayout.getChildAt(i);
        creditCardManageLayout.removeAllViews();
        RollPagerView creditCardManageView = view.findViewById(R.id.creditcard_mainpage_manage);
        creditCardManageView.setAdapter(new StaticPagerAdapter() {
            @Override
            public View getView(ViewGroup container, int position) {
                return creditCardManagePages[position];
            }

            @Override
            public int getCount() {
                return creditCardManagePages.length;
            }
        });
        creditCardManageView.setHintView(new ColorPointHintView(getContext(), getResources().getColor(R.color.colorPrimary), getResources().getColor(R.color.Grey300)));

        /*Toolbar toolbar=view.findViewById(R.id.credit_card_toolbar);
        ((MainActivity)getActivity()).setToolbar(toolbar);
        FrameLayout frameLayout=toolbar.findViewById(R.id.toolbarContainer);
        TextView textView=view.findViewById(R.id.toolbarTitle);*/
        //frameLayout.addView(textView);

        //creditcard_mainpage_header_recommend

        RollPagerView headerRecommend=view.findViewById(R.id.creditcard_mainpage_header_recommend);

        View headerRecommendView=View.inflate(getContext(), R.layout.creditcard_mainpage_header_recommend, null);

        headerRecommend.setAdapter(new StaticPagerAdapter() {

            private int[] cardImgs={
                    R.mipmap.creditcard_mainpage_card_1,R.mipmap.creditcard_mainpage_card_2,
                    R.mipmap.creditcard_mainpage_card_3,R.mipmap.creditcard_mainpage_card_4};

            private String[] cardNames={
                    "LINEFRIENDS卡","龙卡JOY卡","新芭比美丽信用卡","家庭挚爱信用卡"};

            private int[] cardDetails={
                    R.string.creditcard_mainpage_card_1_info,R.string.creditcard_mainpage_card_2_info,
                    R.string.creditcard_mainpage_card_3_info,R.string.creditcard_mainpage_card_4_info};

            @Override
            public View getView(ViewGroup container, int position) {
                View headerRecommendView=View.inflate(getContext(), R.layout.creditcard_mainpage_header_recommend, null);

                ImageView cardImageView=headerRecommendView.findViewById(R.id.creditcard_image);
                TextView cardName=headerRecommendView.findViewById(R.id.creditcard_name);
                TextView cardDetail1=headerRecommendView.findViewById(R.id.creditcard_detail1);
                TextView cardDetail2=headerRecommendView.findViewById(R.id.creditcard_detail2);

                String cardDetail=getResources().getString(cardDetails[position]);

                cardDetail1.setText("●"+cardDetail.split("\n")[0]);
                cardDetail2.setText("●"+cardDetail.split("\n")[1]);

                cardImageView.setImageResource(cardImgs[position]);
                cardName.setText(cardNames[position]);

                return headerRecommendView;
            }

            @Override
            public int getCount() {
                return cardNames.length;
            }
        });

        headerRecommend.setHintView(new ColorPointHintView(getContext(), getResources().getColor(R.color.colorPrimary), getResources().getColor(R.color.Grey300)));

        switchHeaderLayout(1);
        //switchHeaderLayout(0);
    }


    private void switchHeaderLayout(int status) {
        switch (status) {
            case 0:
                headerView[0].setVisibility(View.VISIBLE);
                headerView[1].setVisibility(View.INVISIBLE);
                headerView[2].setVisibility(View.INVISIBLE);
                creditCardDetailsLayout.setVisibility(View.VISIBLE);
                break;
            case 1:
                headerView[0].setVisibility(View.INVISIBLE);
                headerView[1].setVisibility(View.VISIBLE);
                headerView[2].setVisibility(View.INVISIBLE);
                creditCardDetailsLayout.setVisibility(View.GONE);
                break;
            case 2:
                headerView[0].setVisibility(View.INVISIBLE);
                headerView[1].setVisibility(View.INVISIBLE);
                headerView[2].setVisibility(View.VISIBLE);
                creditCardDetailsLayout.setVisibility(View.GONE);
        }
    }

}
